package com.imooc.demo.handler;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by 稻草人 on 2018/6/17.
 * 全局统一异常处理类
 */

@ControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 异常处理类
     * @param req
     * @param e
     * @return
     * ResponseBody注解声明返回的是错误信息而不是html页面
     */
    @ExceptionHandler(value=Exception.class)
    @ResponseBody
    private Map<String,Object> exceptionHandler(HttpServletRequest req, Exception e){
        Map<String,Object> modelMap=new HashMap<String,Object>();
        modelMap.put("success",false);
        modelMap.put("errMsg",e.getMessage());
        return modelMap;
    }



}













